/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hhx.web;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hhx.entity.HhxFinance;
import com.thinkgem.jeesite.modules.hhx.service.HhxFinanceService;

/**
 * 单表生成Controller
 * @author zmy
 * @version 2019-04-12
 */
@Controller
@RequestMapping(value = "${adminPath}/hhx/hhxFinance")
public class HhxFinanceController extends BaseController {

	@Autowired
	private HhxFinanceService hhxFinanceService;
	
	@ModelAttribute
	public HhxFinance get(@RequestParam(required=false) String id) {
		HhxFinance entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxFinanceService.get(id);
		}
		if (entity == null){
			entity = new HhxFinance();
		}
		return entity;
	}
	
	@RequiresPermissions("hhx:hhxFinance:view")
	@RequestMapping(value = {"list", ""})
	public String list(HhxFinance hhxFinance, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxFinance> page = hhxFinanceService.findPage(new Page<HhxFinance>(request, response), hhxFinance); 
		model.addAttribute("page", page);
		return "modules/hhx/hhxFinanceList";
	}

	@RequiresPermissions("hhx:hhxFinance:view")
	@RequestMapping(value = "form")
	public String form(HhxFinance hhxFinance, Model model) {
		model.addAttribute("hhxFinance", hhxFinance);
		return "modules/hhx/hhxFinanceForm";
	}

	@RequiresPermissions("hhx:hhxFinance:edit")
	@RequestMapping(value = "save")
	public String save(HhxFinance hhxFinance, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, hhxFinance)){
			return form(hhxFinance, model);
		}
		hhxFinanceService.save(hhxFinance);
		addMessage(redirectAttributes, "保存融资成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxFinance/?repage";
	}
	
	@RequiresPermissions("hhx:hhxFinance:edit")
	@RequestMapping(value = "delete")
	public String delete(HhxFinance hhxFinance, RedirectAttributes redirectAttributes) {
		hhxFinanceService.delete(hhxFinance);
		addMessage(redirectAttributes, "删除融资成功");
		return "redirect:"+Global.getAdminPath()+"/hhx/hhxFinance/?repage";
	}

	@RequestMapping(value = {"wxlist", ""})
	public String wxlist(HhxFinance hhxFinance, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<HhxFinance> page = hhxFinanceService.findPage(new Page<HhxFinance>(request, response), hhxFinance); 
		return renderString(response, page);
	}
	
	@RequestMapping(value = {"wxget", ""})
	public String wxget(HttpServletRequest request, HttpServletResponse response, @RequestParam(required=false) String id) {
		HhxFinance entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = hhxFinanceService.get(id);
		}
		if (entity == null){
			entity = new HhxFinance();
		}
		return renderString(response, entity);
	}
	
	@RequestMapping(value= {"wxsave",""})
	public String wxsave(HttpServletRequest request, HttpServletResponse response, String userid,String trade,String description,String year1,String income1,String profit1,String year2,String income2,String profit2,String plan,String type,String organization,String ismerge,String extra,String amt) {
		String id = UUID.randomUUID().toString().replace("-", "");
		int result = hhxFinanceService.insertFinance(id,userid,trade,description,year1,income1,profit1,year2,income2,profit2,plan,type,organization,ismerge,extra,amt);
		Map<String, String> map = new HashMap<String, String>();
		if (result>0) {
			map.put("result","true");
			map.put("msg","保存成功");
		}else {
			map.put("result","false");
			map.put("msg","保存失败");
		}
		return renderString(response, map);
	}
}